package com.crawler.util;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.alibaba.druid.pool.DruidDataSource;
import com.crawler.dao.DButils;

public class DruidUtil {
	private volatile static DruidDataSource dataSource =  null;
	private DruidUtil(){
	}
	
	public static DruidDataSource getDataSource() throws SQLException{
		if(dataSource==null){
			synchronized(DruidUtil.class){
				if(dataSource==null){
					dataSource = new DruidDataSource();
					dataSource.setDriverClassName("com.mysql.jdbc.Driver");
					dataSource.setUsername("root"); 
					dataSource.setPassword("123456");
					dataSource.setUrl("jdbc:mysql://127.0.0.1/book3?useUnicode=true&characterEncoding=utf-8&useSSL=true"); 
					dataSource.setInitialSize(16); 
					dataSource.setMinIdle(1); 
					dataSource.setMaxActive(64);	
					dataSource.setMaxWait(30000);
					dataSource.setMinEvictableIdleTimeMillis(40000);
					dataSource.setPoolPreparedStatements(false);						
				}
			}
		}
		return dataSource;		
	}
//	static{
//		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
//		dataSource.setUsername("root"); 
//		dataSource.setPassword("123456");
//		dataSource.setUrl("jdbc:mysql://127.0.0.1/book3?useUnicode=true&characterEncoding=utf-8&useSSL=true"); 
//		dataSource.setInitialSize(5000); 
//		dataSource.setMinIdle(1000); 
//		dataSource.setMaxActive(10000);	
//		dataSource.setMaxWait(30000);
//		dataSource.setMinEvictableIdleTimeMillis(40000);
//		dataSource.setPoolPreparedStatements(false);		
//	}
//	
//	public static DruidDataSource getDataSource(){
//		if(dataSource!=null){
//			return dataSource;
//		}else {
//			dataSource.setDriverClassName("com.mysql.jdbc.Driver");
//			dataSource.setUsername("root"); 
//			dataSource.setPassword("123456");
//			dataSource.setUrl("jdbc:mysql://127.0.0.1/book3?useUnicode=true&characterEncoding=utf-8&useSSL=true"); 
//			dataSource.setInitialSize(5000); 
//			dataSource.setMinIdle(1000); 
//			dataSource.setMaxActive(10000);	
//			dataSource.setMaxWait(30000);
//			dataSource.setMinEvictableIdleTimeMillis(40000);
//			dataSource.setPoolPreparedStatements(false);	
//			return dataSource;
//		}
//	}
	
	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		int count  = 100000;
		CountDownLatch latch = new CountDownLatch(count);
		ExecutorService executorService = Executors.newFixedThreadPool(64);
		for (int i = 0; i < count; i++) {
//			insertBoookName("测试11","测试作者11");
			Book book = new Book();
			book.setLatch(latch);
			book.setName("测试姓名"+i);
			book.setAuthor("测试作者"+i);;
			executorService.execute(book);
		}
		try {
			latch.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}finally{
			executorService.shutdown();
		}
		long end = System.currentTimeMillis();
		System.out.println("耗时:"+(new BigDecimal(end-start)).divide(new BigDecimal(1000L)));
	}
}

class Book implements Runnable{
private CountDownLatch latch;
private String name;
private String author;


@Override
public void run() {
	DButils.insertBoookName(name,author);
	latch.countDown();
}

public CountDownLatch getLatch() {
	return latch;
}

public void setLatch(CountDownLatch latch) {
	this.latch = latch;
}

public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public String getAuthor() {
	return author;
}

public void setAuthor(String author) {
	this.author = author;
}
}
